package no.testspill;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.media.MediaPlayer;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class Ball extends View {
	MediaPlayer sound;
	private int posX ,posY;
	private float dX, dY;
	private double velocity;
	private Bitmap image;
	public final int BALLSIZE = 28; //img is 48x48 px
	//public final int BALLSIZE = 23;   //for testing with emulator
	//view sizes
	private int maxHeight = 0;
	private int maxWidth = 0;

	public Ball(Context context) {
		super(context);
		velocity = 7;
		//initPos(context); removed because of Canvas for correct positioning
		image = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
	}
	
	public void setMaxHeight(int h){
		maxHeight = h;
	}
	
	public void setMaxWidth(int h){
		maxWidth = h;
	}
	
	public int getMaxHeight(){
		return maxHeight;
	}
	
	public int getMaxWidth(){
		return maxWidth;
	}
	
	public int getPosX(){
		return posX;
	}
	
	public int getPosY(){
		return posY;
	}
	
	public void setPosX(int x){
		posX = x;
	}
	
	public void setPosY(int y){
		posY = y;
	}
	
	public double getVelocity(){
		return velocity;
	}
	
	public double getDx(){
		return dX;
	}
	
	public double getDy(){
		return dY;
	}
	
	public void setDx(int dX){
		this.dX = dX;
	}
	
	public void setDy(int dY){
		this.dY = dY;
	}
	
	public Bitmap getImage(){
		return image;
	}
	
	public void setRandomDirecion(Direction dir){
		dX = (int) (Math.random()*4) + 1;
		dY = (int) (Math.random()*4) + 1;
		dY*= (dir==Direction.UP)? 1: -1;
		normalize(); 
		
		System.out.println("SetRandomDirection");
		System.out.println("X: "+ dX);
		System.out.println("Y: "+ dY);
	}
	
	
	
	private void normalize() {
		double vel = (double) Math.sqrt(Math.pow(dX, 2)+Math.pow(dY, 2));
		dX /=vel;
		dY /=vel;
		dX *= velocity;
		dY *= velocity;
		System.out.println("Normalize:");
		System.out.println(velocity);
		System.out.println("X: "+ dX);
		System.out.println("Y: "+ dY);
		
	}
	
	public void bounce(Direction dir){
		dX = (dir == Direction.LEFT || dir==Direction.RIGHT)?dX*(-1):dX;
		dY = (dir == Direction.UP || dir==Direction.DOWN)?dY*(-1):dY;
	}
	
	//Fikse denne metoden senere
	
//	public boolean collision(Canvas c, Controller c1, Controller c2){
//		if(posX <= 0 || posX >= maxWidth-image.getHeight()){//Math.abs(posX-maxWidth) < BALLSIZE){
//			bounce(Direction.LEFT);
//			return false;
//		}
//		if(hitBottomController(c1) || hitTopController(c2)){
//			playControllerHitSound();
//			System.out.println("HIT CONTROLLER!");
//			bounce(Direction.UP);
//			return false;
//		}
//		if(posY<=0|| posY >= maxHeight - image.getHeight()){//Math.abs(posY-maxHeight) < BALLSIZE){
//			//bounce(Direction.UP);
//			return true;
//		}
//		return false;
//	}
//	
//	private boolean hitTopController(Controller c2) {
//		
//		if(posX >= c2.getPosX()-(image.getHeight()) && posX <= c2.getPosX()+c2.getImage().getWidth()-(image.getHeight())){
//			if(Math.abs(posY - (c2.getPosY()+c2.getImage().getHeight())) < 2){
//				return true;
//			}
//		}
//		return false;
//	}
//
//	private boolean hitBottomController(Controller c1) {
//		// TODO Auto-generated method stub
//		if(posX >= c1.getPosX()-(this.BALLSIZE/2) && posX <= c1.getPosX()+c1.CONTROLLERWIDTH-(this.BALLSIZE/2)){
//			if(Math.abs(posY - (c1.getPosY()-BALLSIZE)) < 2){
//				return true;
//			}
//		}
//		return false;
//	}
//
//	//Bare setter en posisjon!!
//	public void initPos(Context c, Canvas canvas, int h, int w){
//		//Display display = ((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
//		//int width = display.getWidth();
//		//int height = display.getHeight();
//		setMaxHeight(h);
//		setMaxWidth(w);
//		
//		//posX = (int) width/2; 
//		//posY = (int) height/2;
//		posX = (int) w/2;
//		posY = (int) h/2;
//		setRandomDirecion(Direction.UP);
//	}
//	
//	/**
//	 * 
//	 * @param c
//	 * @param c1
//	 * @param c2
//	 * @return true if ball is out of play.
//	 */
//	public boolean update(Canvas c, Controller c1, Controller c2){
//		
//		posX+=dX;
//		posY+=dY;
//		return collision(c, c1, c2);
//	}
//
//	public void setVelocity(double velocity) {
//		this.velocity = velocity;
//	}
//	
//	public void playControllerHitSound(){
//		sound = MediaPlayer.create(getContext(), R.raw.pong_controller);
//		sound.start();
//	}

}
